package cn.wps.sdk.demo;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import cn.wps.moffice.sdk.WpsSdk;
import cn.wps.moffice.sdk.api.bean.Result;
import cn.wps.moffice.sdk.api.callback.ResultCallback;
import cn.wps.moffice.sdk.api.common.FileManagerApi;
import cn.wps.sdk.demo.activity.NewDocumentActivity;
import cn.wps.sdk.demo.activity.SingleOpenActivity;
import cn.wps.sdk.demo.aidl.AIDLActivity;
import cn.wps.sdk.demo.util.FileUtil;
import cn.wps.sdk.demo.R;

/**
 * 验证WPS SDK能力入口
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
        findViewById(R.id.fileAssistantSingleFileBt).setOnClickListener(v -> {
            openSingleDocument();
        });
        findViewById(R.id.btn_newFile).setOnClickListener(view -> newDocument());
        findViewById(R.id.btn_aidl).setOnClickListener(view -> testAidl());
        findViewById(R.id.btn_select_files).setOnClickListener(view -> selectWpsFiles());
    }

    private void selectWpsFiles() {
        FileManagerApi fileManagerApi = WpsSdk.getInstance().getApi(FileManagerApi.class);
        if (fileManagerApi != null) {
            fileManagerApi.selectFiles(MainActivity.this, new ResultCallback<Uri>() {
                @Override
                public void onCallback(Result<Uri> result) {
                    if (result.code == 0) {
                        Uri fileUri = result.data;
                        Log.d("WpsSdkLog", "MainActivity selectFiles onCallback 转换输出路径：fileUri=" + fileUri);
                        //从WPS路径拷贝到第三方应用路径
                        String outFile = FileUtil.copyFileFromUri(MainActivity.this, fileUri, "output");
                        Log.d("WpsSdkLog", "MainActivity selectFiles onCallback: outFile=" + outFile);
                        Toast.makeText(MainActivity.this, " 选择的文件路径：" + fileUri, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, " 出现异常：" + result.msg, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }


    private void testAidl() {
        Intent intent = new Intent();
        intent.setClass(this, AIDLActivity.class);
        startActivity(intent);
    }


    private void newDocument() {
        Intent intent = new Intent();
        intent.setClass(this, NewDocumentActivity.class);
        startActivity(intent);
    }

    /**
     * 打开单文件
     */
    private void openSingleDocument() {
        Intent intent = new Intent();
        intent.setClass(this, SingleOpenActivity.class);
        startActivity(intent);
    }
}